var userId = GetQueryString("userId");
var userInfo
$(function () {
    var getRequest = GetRequest();
    var code = getRequest.code;
    if (code) {
        $.ajax({
            type: "POST",
            cache: false,
            data: {code: code},
            async: false,
            url: getWeiContextPath() + "/wx/VipUser/toInsert",
            success: function (result) {
               userId = result.data.id
            },
            error: function (result) {
                $.toast("网络走丢了","forbidden")
            }
        });
    }
    if (userId) {    //有值
        getUserUse()
        if (userId == 0){
            $('#userInfo').hide();
            $('#unInsert').show();
            $('.weui_grids').hide()
        }else {
            userInfo = getUserInfo(userId);
            $('.userNickName').text(userInfo.nickName)
            $('.userAvatarUrl').attr("src",userInfo.avater)
            $('.jifen').text(userInfo.jiFen)
            $('.ownMoney').text(userInfo.ownMoney)
            if (userInfo.phoneNum){
                $('.phone').text(userInfo.phoneNum)
                queryMyCouponNum();
            }else {
                window.location.href = "phone.html?userId="+userId;
            }
            $('#userInfo').show();
            toUse();
        }
    }else {    //无值返回
        window.history.back(-1);
    }
    getMenu();
})
/*正式授权*/
function toInsert() {
    var pageUrl = window.location.href
        .replace(/[/]/g, "%2f")
        .replace(/[:]/g, "%3a")
        .replace(/[#]/g, "%23")
        .replace(/[&]/g, "%26")
        .replace(/[=]/g, "%3d");
    var url =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
        getWeixinConfig().APPID +
        "&redirect_uri=" +
        pageUrl +
        "&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
    window.location.href = url;
}

/*查询我的可用优惠券数量*/
function queryMyCouponNum() {
    $.post(getWeiContextPath()+"/wx/VipUser/queryMyCouponNum",{wxUserId:userId},function (res) {
        $('#couponNum').text(res)
    })
}

/*查看商铺的结果*/
function lookResult() {
    $.post(getWeiContextPath()+"/wx/shop/queryShopByUid",{wxUserId:userId},function (res) {
        if (res){     //有(审核中或者驳回)记录
            window.location.href = "process.html?userId="+userId
        }else {    //无申请记录
            window.location.href = "changeShop.html?userId="+userId;
        }
    })
}

/*进入商家*/
function toShopList() {
    $.post(getWeiContextPath()+"/wx/shop/toShopListByWid",{wxUserId:userId},function (res) {
        console.log(res)
        $.actions({
            actions:res.map(function (item) {
                return {
                    text:item.shopName,
                    onClick:(function (id) {
                        return function () {
                            window.location.href="shop/index.html?shopId="+id+"&userId="+userId;
                        }
                    })(item.shopId)
                }
            })
        });
    })
}

/*获取用户功能*/
function getUserUse() {
    $('.merchant-top').html('<a href="javascript:void (0)">\n' +
        '            <div class="cl-main"><span class="jifen">0</span></div>\n' +
        '            <div>积分</div>\n' +
        '        </a>\n' +
        '        <a href="userPick.html?userId='+userId+'">\n' +
        '            <div class="cl-main"><span class="ownMoney">0</span></div>\n' +
        '            <div>账户余额</div>\n' +
        '        </a>\n' +
        '        <a href="yhq.html?userId='+userId+'">\n' +
        '            <div class="cl-main" id="couponNum">0</div>\n' +
        '            <div>优惠券</div>\n' +
        '        </a>')
}
/*形成底部菜单*/
function getMenu() {
    $('.weui-tabbar').html('<a href="index.html?userId=' + userId + '" class="weui-tabbar__item">\n' +
        '            <div class="weui-tabbar__icon">\n' +
        '                <img src="images/shouye.png" alt="">\n' +
        '            </div>\n' +
        '            <p class="weui-tabbar__label">首页</p>\n' +
        '        </a>\n' +
        '        <a href="discover.html?userId=' + userId + '" class="weui-tabbar__item">\n' +
        '            <div class="weui-tabbar__icon">\n' +
        '                <img src="images/faxianguanli.png" alt="">\n' +
        '            </div>\n' +
        '            <p class="weui-tabbar__label">发现</p>\n' +
        '        </a>\n' +
        '        <a href="cellect.html?userId=' + userId + '" class="weui-tabbar__item">\n' +
        '            <div class="weui-tabbar__icon">\n' +
        '                <img src="images/order.png" alt="">\n' +
        '            </div>\n' +
        '            <p class="weui-tabbar__label">收藏</p>\n' +
        '        </a>\n' +
        '        <a href="javascript:void (0)" class="weui-tabbar__item weui-bar__item--on">\n' +
        '            <div class="weui-tabbar__icon">\n' +
        '                <img src="images/me1.png" alt="">\n' +
        '            </div>\n' +
        '            <p class="weui-tabbar__label">我的</p>\n' +
        '        </a>')
}
/*形成常用功能页面*/
function toUse() {
    var str='';
    $.ajax({
        type: "POST",
        cache: false,
        data: {wxUserId:userInfo.id},
        async: false,
        url: getWeiContextPath()+"/wx/VipUser/myMessageCount",
        success: function (res) {
            str ='<a href="my-news.html?userId='+userId+'" class="weui_grid js_grid" style="position: relative;">\n' +
                '            <span class="weui-badge" style="position: absolute;top:0;right:15px;">'+res+'</span>\n' +
                '            <div class="weui_grid_icon">\n' +
                '                <img style="width:24px;height:24px;" src="images/xiaoxi11.png" alt="">\n' +
                '            </div>\n' +
                '            <p class="weui_grid_label">\n' +
                '                我的消息\n' +
                '            </p>\n' +
                '        </a>'
        },
    });
    $('.weui_grids').html('<a href="order.html?userId='+userId+'" class="weui_grid js_grid">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-dingdan"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                我的订单\n' +
        '            </p>\n' +
        '        </a>\n' +
        '        <a href="exchange.html?userId='+userId+'" class="weui_grid js_grid">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-jifen"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                积分兑换\n' +
        '            </p>\n' +
        '        </a>\n' +
        '        <a href="clause.html" class="weui_grid js_grid">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-shoucang-"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                使用说明\n' +
        '            </p>\n' +
        '        </a>\n' +
        '        <a href="javascript:void (0)" class="weui_grid js_grid" onclick="lookResult()">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-permissions-user"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                成为商家\n' +
        '            </p>\n' +
        '        </a>\n' +
        '        <a href="javascript:void (0)" class="weui_grid js_grid" onclick="toShopList()">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-tubiaolunkuo-"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                商家端口\n' +
        '            </p>\n' +
        '        </a>\n' +
        '        <a href="help.html?userId='+userId+'" class="weui_grid js_grid">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-fankui"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                帮助与反馈\n' +
        '            </p>\n' +
        '        </a>' +
        '        <a href="javascript:void (0)" class="weui_grid js_grid" onclick="toPassword()">\n' +
        '            <div class="weui_grid_icon">\n' +
        '                <i class="iconfont icon-dingdan2"></i>\n' +
        '            </div>\n' +
        '            <p class="weui_grid_label">\n' +
        '                密码中心\n' +
        '            </p>\n' +
        '        </a>' +str+
        '')
}

function toPassword() {
    if (userInfo.payNumber){
        window.location.href = "ex_pwd.html?userId="+userId;
    }else {
        window.location.href = "set_pwd.html?userId="+userId;
    }
}

